package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.shaoshaohuo.app.R;

import java.util.List;

/**
 * Created by Administrator on 2017/6/29.
 */

public class GridViewAdapter extends BaseAdapter {
    private Context context;
    private List<String> data;

    public GridViewAdapter(Context context, List<String> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        pictureViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.commit_picture_grid, parent, false);
            holder = new pictureViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.commit_picture_item);
            holder.commit_picture_chahao = (ImageView) convertView.findViewById(R.id.commit_picture_chahao);
            convertView.setTag(holder);
        } else {
            holder = (pictureViewHolder) convertView.getTag();
        }
        holder.commit_picture_chahao.setTag(position);
//        holder.imageView.setImageBitmap(BitmapFactory.);
        holder.imageView.setImageBitmap(BitmapFactory.decodeFile(data.get(position)));
        holder.commit_picture_chahao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int tag = (int) v.getTag();
                data.remove(tag);
                notifyDataSetChanged();
                if (delete != null) {
                    delete.delete();
                }
            }
        });
        return convertView;
    }

    Delete delete;

    public void setDelete(Delete delete) {
        this.delete = delete;
    }

    public interface Delete {
        void delete();
    }

    class pictureViewHolder {
        ImageView imageView;
        ImageView commit_picture_chahao;
    }
}
